using System;
using Nsb;
using YoshiBase.Classes;
using jQueryApi;

namespace Yoshi.Classes
{
    public delegate void ControlEventDelegate(jQueryEvent e, string postName);

    public delegate void AreaDelegate(jQueryObject jo);

    public abstract partial class AbstractView : IView
    {
        private jQueryObject _content;
        private jQueryObject _element;
        private IPage _page;

        protected AbstractView(jQueryObject element, IPage page)
        {
            _element = element;
            _page = page;
        }

        protected IPage Page
        {
            get { return _page; }
            set { _page = value; }
        }

        protected jQueryObject Content
        {
            get { return _content; }
            private set { _content = value; }
        }

        #region IView Members

        public jQueryObject Element
        {
            get { return _element; }
            set { _element = value; }
        }

        public virtual void Build()
        {
            Inform.Trace("AbstractView.Build()");
            if (Script.IsNullOrUndefined(Content))
                Content = jQuery.FromHtml("<div class='AbstractViewContent'/>").AppendTo(Element);
        }

        public abstract void Refresh();
        public abstract object ScoopData();

        public virtual bool Saved
        {
            get { return true; }
            set { }
        }

        public virtual void UpdatePage()
        {
            PageUpdate pu = new PageUpdate();
            pu.Saved = Saved;
            Master.Page.UpdatePage(pu);
        }

        #endregion

        public static bool IsValidName(string name)
        {
            return !String.IsNullOrEmpty(CleanName(name));
        }

        public static bool IsValidNames(string name1, string name2)
        {
            return IsValidName(name1) && IsValidName(name2);
        }

        public static string CleanName(string name)
        {
            return Script.IsNullOrUndefined(name) ? "" : name.Trim();
        }

        public void AddToolButton(string face, FnDelegate1 fn)
        {
            jQuery.Select("#toollist").Append(
                jQuery.FromHtml("<li/>").Append(
                    jQuery.FromHtml("<button/>").Html(face).Click(
                        delegate
                        {
                            if (Script.IsValue(fn))
                                fn(null);
                        }
                        )
                    )
                );
        }

        public jQueryObject NewLink(string face, string url)
        {
            jQueryObject lnk = jQuery.FromHtml("<a/>")
                .Attribute("href", url)
                .Html(face)
                ;
            return lnk;
        }

        public void AddToolLink(string face, string url)
        {
            jQuery.Select("#toollist").Append(
                jQuery.FromHtml("<li/>").Append(
                    jQuery.FromHtml("<a target='_blank'/>").Attribute("href", url).Html(face))
                );
        }

        public void AddArea(string id, AreaDelegate fn)
        {
            jQueryObject area = jQuery.FromHtml("<div class='testArea'/>").Attribute("id", id).AppendTo(Content);
            fn(area);
        }

        protected static bool Confirm(string msg)
        {
            return Script.Confirm(msg);
        }

        protected void Write(string msg)
        {
            Content[0].InnerHTML += msg;
        }

        protected static void WriteJo(jQueryObject jo, string msg)
        {
            jo[0].InnerHTML += msg;
        }
    }
}